package com.example.dbdesign.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.dbdesign.mapper.ResultMapper;
import com.example.dbdesign.pojo.entity.Result;
import com.example.dbdesign.pojo.entity.User;
import com.example.dbdesign.pojo.vo.LoginUser;
import com.example.dbdesign.service.ResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ResultServiceImpl implements ResultService {

    @Autowired
    private ResultMapper resultMapper;

    @Override
    public Result add(Result result) {
        resultMapper.add(result);
        Result result1 = resultMapper.queryByRnoAndType(result);
        return result1;
    }

    @Override
    public Result selectByRnoAndType(String rno, int type) {
        Result res = new Result();
        res.setRno(rno);
        res.setType(type);
        Result result = resultMapper.queryByRnoAndType(res);
        return result;
    }

    @Override
    public Result update(Result result){
        JSONObject json = result.getInfo();
        json.put("msg", null);
        Result r = resultMapper.queryById(result.getId());
        r.setInfo(json);
        r.setMaster(result.getMaster());
        r.setReview(0);
//        resultUpdate.setId(result.getId());
//        resultUpdate.setInfo(json);
//        resultUpdate.setMaster(result.getMaster());
        resultMapper.update(r);
        Result voresult = resultMapper.queryById(r.getId());
        if(voresult == null){
            return null;
        }
        return voresult;
    }

    @Override
    public Boolean deleteById(int id){
        resultMapper.deleteById(id);
        return true;
    }

    @Override
    public Result review(Result result){
        if(result.getReview() <= 0)
            resultMapper.update(result);
        else {
            JSONObject json = result.getInfo();
//            json.put("msg", "");
            Result r = resultMapper.queryById(result.getId());
            r.setInfo(json);
            r.setMaster(result.getMaster());
            r.setReview(1);
            resultMapper.update(result);
        }
        Result voresult = resultMapper.queryById(result.getId());
        return voresult;
    }

    @Override
    public Result selectById(int id){
        Result res = resultMapper.queryById(id);
        if(res == null){
            return null;
        }
        return res;
    }

    @Override
    public List<Result> selectByPage(Map<String, Object> params) {
        return resultMapper.queryByPage(params);
    }

    @Override
    public int calSum(){
        return resultMapper.cal();
    }

    @Override
    public List<Result> selectByTypeAndDate(int type, int date){
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("date", date);
        if(type == 0) {
            return selectByDate(date);
        }
        if(date == 0) {
            return selectByType(type);
        }
        return resultMapper.queryByTypeAndDate(map);
    }

    @Override
    public List<Result> selectByType(int type){
        return resultMapper.queryByType(type);
    }

    @Override
    public List<Result> selectByDate(int date){
        return resultMapper.queryByDate(date);
    }

    @Override
    public List<Result> selectByTeacher(){
        LoginUser lu = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(lu.getUser().getRole() == 2){
            return resultMapper.queryByUid(lu.getUser().getId());
        }else{
            return null;
        }
    }

    @Override
    public List<Result> selectByStu(){
        LoginUser lu = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(lu.getUser().getRole() == 3){
            return resultMapper.queryByUidForStu(lu.getUser().getId());
        }else{
            return null;
        }
    }
}
